package com.uinnova.springboot.demo.asynchronization;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author 张攀钦
 * @date 2018-12-21-17:03
 */
@RestController
public class AsyncController {
    @Autowired
    private MsgService msgService;

    @GetMapping(value = "/jail/async/noret")
    public Map<String, Object> getAsyncNoRet() throws Exception {
        Map<String, Object> stringObjectMap = new HashMap<>(16);
        msgService.sendA();
        msgService.sendB();
        stringObjectMap.put("success", true);
        return stringObjectMap;
    }

    @GetMapping(value = "/jail/async/ret")
    public Map<String, Object> getAsyncRet() throws Exception {
        Map<String, Object> ret = new HashMap<>(16);
        Future<String> sendC = msgService.sendC();
        Future<String> sendD = msgService.sendD();
        String sendCRet = sendC.get(5000, TimeUnit.MILLISECONDS);
        String sendDRet = sendD.get(5000, TimeUnit.MILLISECONDS);
        ret.put("success", true);
        ret.put("sendC", sendCRet);
        ret.put("sendD", sendDRet);
        return ret;
    }
}
